﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Expect.VisualStudio.Extensions.Services
{
    using EnvDTE;

    using Microsoft.VisualStudio.Shell;

    public class VsOutputWriter
    {
        private IExpectPackage _pkg;
        OutputWindowPane outputWindowPane;

        public VsOutputWriter(IExpectPackage pkg, string name)
        {
            DTE dte = (DTE)pkg.GetVSService(typeof(DTE));
            Windows windows = dte.Windows;
            Window window = (Window)windows.Item(EnvDTE.Constants.vsWindowKindOutput);
            window.Visible = true;

            OutputWindow outputWindow = (OutputWindow)window.Object;
            foreach (OutputWindowPane pane in outputWindow.OutputWindowPanes)
            {
                if (pane.Name == name)
                    outputWindowPane = pane;
            }

            if (outputWindowPane == null)
            {
                outputWindowPane = outputWindow.OutputWindowPanes.Add(name);
            }

            outputWindowPane.Activate();
        }

        public void Clear()
        {
            outputWindowPane.Clear();
        }

        public void Show()
        {
            outputWindowPane.Activate();
        }

        public void Write(string txt)
        {
            outputWindowPane.OutputString(txt);
        }

        public void WriteLine(string txt)
        {
            outputWindowPane.OutputString(txt + "\r\n");
        }
    }
}
